package com.cdx.spring.juc.printtrhead;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * @author chedx
 * @data 2021/2/28 10:57 AM
 */
public class PrintThread implements Runnable {

    private String currentThread;
    private Condition condition;
    private int num ;
    private Lock lock ;
    private static AtomicInteger i = new AtomicInteger(0);

    public PrintThread(String currentThread, Condition condition, int num, Lock lock) {
        this.currentThread = currentThread;
        this.condition = condition;
        this.num = num;
        this.lock = lock;
    }

    public void run() {
        while (true) {
            try {
                lock.lock();

                while (i.get()%3 != num) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("当前线程是：" + this.currentThread);
                i.getAndIncrement();
                condition.signalAll();

            } finally {
                lock.unlock();
            }



        }
    }
}
